package com.gouaixin.bee.beeframework2.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ab.util.AbImageUtil;
import com.ab.util.AbToastUtil;
import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.model.HelloModel;
import com.gouaixin.bee.beeframework2.tool.PreferencesUtils;
import com.gouaixin.bee.beeframework2.tool.mLog;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class NearAdapter extends BaseAdapter {



	Context context;
//	ArrayList<HashMap<String, Object>> al;
	LayoutInflater mInflater;
    JSONArray jsonArray;

    public NearAdapter(Context context) {
        this.context = context;
//		this.al = al;
        mInflater = LayoutInflater.from(context);
        setJson(new JSONArray());

    }

    public void setJson(JSONArray jsonArray) {
        this.jsonArray = jsonArray;
    }


	public NearAdapter(Context context, ArrayList<HashMap<String, Object>> al) {
		this.context = context;
//		this.al = al;
		mInflater = LayoutInflater.from(context);
		
	}

	public int getCount() {
		return jsonArray.length();
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return position;
	}
	
	public View getView(final int position, View convertView, ViewGroup parent) {

		final ViewHolder viewHolder;
		
		if(convertView == null){
			
			convertView = mInflater.inflate(R.layout.near_item, null);
			viewHolder = new ViewHolder();

			viewHolder.icon = (ImageView) convertView.findViewById(R.id.icon);
			viewHolder.name = (TextView) convertView.findViewById(R.id.name);
			viewHolder.age = (TextView) convertView.findViewById(R.id.age);
			viewHolder.height = (TextView) convertView.findViewById(R.id.height);
			viewHolder.juli = (TextView) convertView.findViewById(R.id.juli);
			viewHolder.dazhaohu = (ImageView) convertView.findViewById(R.id.dazhaohu);

			convertView.setTag(viewHolder);

		}else {

			viewHolder = (ViewHolder) convertView.getTag();
		}
        try {
            if (jsonArray.length() > 0) {


                JSONObject jsonOb = (JSONObject) jsonArray.opt(position);
                if (jsonOb != null) {
                    String icon = jsonOb.getString("icon");
                    ImageLoader.getInstance().displayImage(icon,new ImageView(context),new ImageLoadingListener() {
                        @Override
                        public void onLoadingStarted(String s, View view) {

                        }

                        @Override
                        public void onLoadingFailed(String s, View view, FailReason failReason) {

                        }

                        @Override
                        public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                            viewHolder.icon.setBackgroundDrawable(AbImageUtil.bitmapToDrawable(bitmap));
                        }

                        @Override
                        public void onLoadingCancelled(String s, View view) {

                        }
                    });

                    viewHolder.name.setText(jsonOb.getString("name"));

                    viewHolder.height.setText(jsonOb.getString("height"));

                    viewHolder.age.setText(jsonOb.getString("age"));

                    viewHolder.juli.setText(jsonOb.getString("juli"));

                    final String jid = jsonOb.getString("id");
                    final String jicon = jsonOb.getString("icon");
                    final String jname = jsonOb.getString("name");

                    final boolean isZhaohu = PreferencesUtils.getBoolean(context, jid + "isZhaohu", false);
                    final boolean isGuanzhu = PreferencesUtils.getBoolean(context, jid + "isGuanzhu", false);
                    final String cid = PreferencesUtils.getString(context, "cid");

                    viewHolder.dazhaohu.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
//                            mLogUtil.e("进入更新昵称界面");
//                Intent it = new Intent().setClass(context, B20_gengxinNichengActivity.class);
//                context.startActivity(it);

                            mLog.e("点击了打招呼");
                            mLog.e("position=" + position);
                            mLog.e("isZhaohu=" + isZhaohu);
                            mLog.e("jid=" + jid);
                            mLog.e("jicon=" + jicon);
                            mLog.e("cid=" + cid);
                            mLog.e("jname=" + jname);
                            if (isZhaohu == false) {
                                HelloModel.oneZhaohu(context, jid, jicon, cid, jname);
                                AbToastUtil.showToast(context, "打招呼成功！");
                                notifyDataSetChanged();
                            } else {
                                AbToastUtil.showToast(context, "已打招呼！");
                            }
                        }
                    });

                    if (isZhaohu == false) {
                        viewHolder.dazhaohu.setBackgroundResource(R.drawable.nearyby_sayhi);
                    } else {
                        viewHolder.dazhaohu.setBackgroundResource(R.drawable.nearyby_sayhi_des);
                    }


                }


            }
        } catch (JSONException e) {
            e.printStackTrace();
        }


//        int a=new Random().nextInt(100)+10;





		return convertView;
	}
	/**
	 * 视图控制类
	 */
	class ViewHolder {
        ImageView icon;
		TextView name;
		TextView age;
		TextView height;
		TextView juli;
		ImageView dazhaohu;
	}
	
	
	
	
	
}
